<script setup>
import { ref, onMounted, onUnmounted, computed, watch, getCurrentInstance } from 'vue'
import * as echarts from 'echarts'
import dayjs from 'dayjs'
import { getEchartData } from '@/axios/api/statistics'


const { proxy } = getCurrentInstance();

// 统计维度选项
const statisticsDimensions = [
  { value: 'violation', label: '违规信息统计' },
  { value: 'blacklist', label: '黑名单人员统计' },
  { value: 'hotspot', label: '热点信息统计' },
  { value: 'posters', label: '热门发帖人统计' },
  { value: 'topics', label: '热门主题版块统计' }
]

// 时间范围
const timeRange = ref('week')
const selectedDimension = ref('violation')
const loading = ref(false)

const setDate = () => {
  if (timeRange.value === 'quarter') {
    return dayjs().subtract(90, 'day').format('YYYY-MM-DD')
  }
  return dayjs().subtract(1, timeRange.value).format('YYYY-MM-DD')
}

// Mock数据 - 违规信息统计
const violationData = ref({
  total: 0,
  categories: [
    // { name: '政治敏感', value: 45, percentage: 28.8 },
    // { name: '暴力内容', value: 32, percentage: 20.5 },
    // { name: '色情内容', value: 28, percentage: 17.9 },
    // { name: '虚假信息', value: 25, percentage: 16.0 },
    // { name: '恶意广告', value: 18, percentage: 11.5 },
    // { name: '其他违规', value: 8, percentage: 5.1 }
  ]
})

const getViolation = async () => {
  loading.value = true
  const res = await getEchartData('/forum-statistics/violation', {
    startDate: setDate(),
    endDate: dayjs().format('YYYY-MM-DD')
  })
  loading.value = false
  if (res.code === 0 && res.data) {
    violationData.value = res.data
    violationData.value.categories = res.data.violationTypes.map(item => {
      return {
        name: item.name,
        value: item.value,
        percentage: (item.value / res.data.total * 100).toFixed(2)
      }
    })
  }
}

// Mock数据 - 黑名单人员统计
const blacklistData = ref({
  total: 0,
  // permanent: 23,
  // temporary: 66,
  disabledUsers: 0,
  mutedUsers: 0,
  reasons: [
    // { name: '多次违规', value: 35, percentage: 39.3 },
    // { name: '恶意刷屏', value: 28, percentage: 31.5 },
    // { name: '发布虚假信息', value: 15, percentage: 16.9 },
    // { name: '人身攻击', value: 11, percentage: 12.4 }
  ],
  // trend: [
  //   { date: '2025-07-24', permanent: 2, temporary: 8 },
  //   { date: '2025-07-25', permanent: 1, temporary: 12 },
  //   { date: '2025-07-26', permanent: 3, temporary: 9 },
  //   { date: '2025-07-27', permanent: 2, temporary: 15 },
  //   { date: '2025-07-28', permanent: 4, temporary: 11 },
  //   { date: '2025-07-29', permanent: 1, temporary: 18 },
  //   { date: '2025-07-30', permanent: 10, temporary: 23 }
  // ],
  series: {}
})

const getBlacklist = async () => {
  loading.value = true
    const res = await getEchartData('/forum-statistics/blacklist', {
    startDate: setDate(),
    endDate: dayjs().format('YYYY-MM-DD')
  })
  loading.value = false
  if (res.code === 0 && res.data) {
      const data = res.data
      blacklistData.value.series = data.chart
      blacklistData.value.total = data.totalBlacklistUsers
      blacklistData.value.disabledUsers = data.disabledUsers
      blacklistData.value.mutedUsers = data.mutedUsers

      blacklistData.value.reasons = [
        { name: '禁言', value: data.mutedUsers },
        { name: '禁用', value: data.disabledUsers }
      ]
  }
}

// Mock数据 - 热点信息统计
const hotspotData = ref({
  posts: [
    // { title: '民航安全新规解读', reads: 15680, comments: 892, shares: 456, likes: 2341 },
    // { title: '飞行事故案例分析', reads: 12450, comments: 678, shares: 234, likes: 1892 },
    // { title: '飞行员培训标准更新', reads: 9870, comments: 445, shares: 189, likes: 1456 },
    // { title: '机场安全管理实践', reads: 8760, comments: 389, shares: 167, likes: 1234 },
    // { title: '航空器维护保养指南', reads: 7650, comments: 312, shares: 145, likes: 987 }
  ],
  // trend: [
  //   { date: '2025-07-24', reads: 12500, comments: 680, shares: 320 },
  //   { date: '2025-07-25', reads: 13800, comments: 720, shares: 350 },
  //   { date: '2025-07-26', reads: 15200, comments: 810, shares: 390 },
  //   { date: '2025-07-27', reads: 16800, comments: 890, shares: 420 },
  //   { date: '2025-07-28', reads: 14500, comments: 780, shares: 380 },
  //   { date: '2025-07-29', reads: 16200, comments: 850, shares: 410 },
  //   { date: '2025-07-30', reads: 18900, comments: 920, shares: 450 }
  // ],
  series: {}
})

const getHotPost = async () => {
  loading.value = true
  
    const res = await getEchartData('/forum-statistics/hot-post', {
    startDate: setDate(),
    endDate: dayjs().format('YYYY-MM-DD')
  })
  loading.value = false
  if (res.code === 0 && res.data) {
      const data = res.data
      hotspotData.value.series = data.chart
      hotspotData.value.series.series.map(v => {
        v.stack = ''
        return v
      })
      hotspotData.value.posts = data && data.hotPosts.map(v => {
        return {
          title: v.title,
          reads: v.readCount,
          comments: v.commentCount,
          likes: v.forwardCount,
        }
      })
  }
}

// Mock数据 - 热门发帖人统计
const postersData = ref({
  users: [
    // { name: '张飞行', posts: 45, likes: 2341, comments: 892, followers: 15680, avatar: 'https://via.placeholder.com/40' },
    // { name: '李机长', posts: 38, likes: 1892, comments: 678, followers: 12450, avatar: 'https://via.placeholder.com/40' },
    // { name: '王安全', posts: 32, likes: 1456, comments: 445, followers: 9870, avatar: 'https://via.placeholder.com/40' },
    // { name: '赵维护', posts: 28, likes: 1234, comments: 389, followers: 8760, avatar: 'https://via.placeholder.com/40' },
    // { name: '刘培训', posts: 25, likes: 987, comments: 312, followers: 7650, avatar: 'https://via.placeholder.com/40' }
  ],
  // trend: [
  //   { date: '2025-07-24', posts: 156, likes: 8900, comments: 3200 },
  //   { date: '2025-07-25', posts: 168, likes: 9200, comments: 3400 },
  //   { date: '2025-07-26', posts: 145, likes: 8100, comments: 2900 },
  //   { date: '2025-07-27', posts: 189, likes: 10500, comments: 3800 },
  //   { date: '2025-07-28', posts: 172, likes: 9600, comments: 3500 },
  //   { date: '2025-07-29', posts: 198, likes: 11200, comments: 4100 },
  //   { date: '2025-07-30', posts: 234, likes: 12800, comments: 4600 }
  // ]
})

const getHotPoster = async () => {
  loading.value = true
     const res = await getEchartData('/forum-statistics/hot-poster', {
    startDate: setDate(),
    endDate: dayjs().format('YYYY-MM-DD')
  })
  loading.value = false
  if (res.code === 0 && res.data) {
      const data = res.data
       postersData.value.users = data.source.map(item => {
        return {
          name: item.product,
          posts: item['发帖数'],
          likes: item['点赞数'],
          comments: item['评论数'],
        }
      })
  }
}

// Mock数据 - 热门主题版块统计
const topicsData = ref({
  topics: [
    // { name: '民航安全', posts: 2341, users: 15680, activity: 89.5, growth: 12.3 },
    // { name: '飞行技术', posts: 1892, users: 12450, activity: 76.8, growth: 8.7 },
    // { name: '事故分析', posts: 1456, users: 9870, activity: 65.2, growth: 15.4 },
    // { name: '培训教育', posts: 1234, users: 8760, activity: 58.9, growth: 6.2 },
    // { name: '维护保养', posts: 987, users: 7650, activity: 45.3, growth: 9.8 }
  ],
  // trend: [
  //   { date: '2025-07-24', posts: 156, users: 8900, activity: 78.5 },
  //   { date: '2025-07-25', posts: 168, users: 9200, activity: 82.1 },
  //   { date: '2025-07-26', posts: 145, users: 8100, activity: 71.3 },
  //   { date: '2025-07-27', posts: 189, users: 10500, activity: 88.7 },
  //   { date: '2025-07-28', posts: 172, users: 9600, activity: 79.2 },
  //   { date: '2025-07-29', posts: 198, users: 11200, activity: 91.4 },
  //   { date: '2025-07-30', posts: 234, users: 12800, activity: 95.8 }
  // ]
})

const getHotTopic = async () => {
  loading.value = true
    const res = await getEchartData('/forum-statistics/hot-topic', {
    startDate: setDate(),
    endDate: dayjs().format('YYYY-MM-DD')
  })
  loading.value = false
  if (res.code === 0 && res.data) {
      const data = res.data
        topicsData.value.topics = data.topTopics.map(item => {
        return {
          name: item.topicName,
          posts: item.postCount,
          users: item.userCount,
          activity: item.activityScore
        }
      })
  }
}


// 图表实例
let violationChart = null
let blacklistChart = null
let hotspotChart = null
let postersChart = null
let topicsChart = null
let blacklistreasonsChart = null

// 监听tab切换，重新初始化对应的图表
watch(selectedDimension, (newDimension) => {
  // 延迟执行，确保DOM已更新
  setTimeout(() => {
    switch (newDimension) {
      case 'violation':
        initViolationChart()
        break
      case 'blacklist':
        initBlacklistChart()
        break
      case 'hotspot':
        initHotspotChart()
        break
      case 'posters':
        initPostersChart()
        break
      case 'topics':
        initTopicsChart()
        break
    }
  }, 200)
})

// 初始化违规信息统计图表
const initViolationChart = async () => {
  await getViolation()
  const chartDom = document.getElementById('violationChart')
  if (!chartDom) {
    console.warn('violationChart DOM element not found')
    return
  }

  try {
    // 如果已存在实例，先销毁
    if (violationChart) {
      violationChart.dispose()
      violationChart = null
    }

    violationChart = echarts.init(chartDom)

    const option = {
      title: {
        text: '违规信息类型分布',
        left: 'center',
        top: 10,
        textStyle: { fontSize: 16, fontWeight: 'bold' }
      },
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)'
      },
      legend: {
        orient: 'vertical',
        left: 'left',
        top: 60,
        data: violationData.value.categories.map(item => item.name)
      },
      series: [
        {
          name: '违规类型',
          type: 'pie',
          radius: ['40%', '70%'],
          center: ['60%', '60%'],
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '18',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: violationData.value.categories
        }
      ]
    }

    violationChart.setOption(option)
  } catch (error) {
    console.error('初始化违规信息图表失败:', error)
  }
}

// 初始化黑名单人员统计图表
const initBlacklistChart = async () => {
  await getBlacklist()
  const chartDom = document.getElementById('blacklistChart')
  if (!chartDom) {
    console.warn('blacklistChart DOM element not found')
    return
  }

  try {
    // 如果已存在实例，先销毁
    if (blacklistChart) {
      blacklistChart.dispose()
      blacklistChart = null
    }

    blacklistChart = echarts.init(chartDom)

    const option = {
      title: {
        text: '黑名单人员统计',
        left: 'center',
        top: 10,
        textStyle: { fontSize: 16, fontWeight: 'bold' }
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { type: 'cross' }
      },
      legend: {
        data: blacklistData.value.series.legend,
        top: 40
      },
      grid: {
        top: 80,
        left: 60,
        right: 40,
        bottom: 40
      },
      xAxis: {
        type: 'category',
        data: blacklistData.value.series.xaxis
      },
      yAxis: {
        type: 'value'
      },
      series: blacklistData.value.series.series
      // [
      //   {
      //     name: '永久封禁',
      //     type: 'line',
      //     data: blacklistData.value.trend.map(item => item.permanent),
      //     itemStyle: { color: '#ff4757' }
      //   },
      //   {
      //     name: '临时封禁',
      //     type: 'line',
      //     data: blacklistData.value.trend.map(item => item.temporary),
      //     itemStyle: { color: '#ffa502' }
      //   }
      // ]
    }
    initblacklistreasonsChart()

    blacklistChart.setOption(option)
  } catch (error) {
    console.error('初始化黑名单图表失败:', error)
  }
}

const initblacklistreasonsChart = async () => {
  const chartDom = document.getElementById('blacklistreasonsChart')
  if (!chartDom) {
    console.warn('violationChart DOM element not found')
    return
  }

  try {
    // 如果已存在实例，先销毁
    if (blacklistreasonsChart) {
      blacklistreasonsChart.dispose()
      blacklistreasonsChart = null
    }

    blacklistreasonsChart = echarts.init(chartDom)

    const option = {
      title: {
        text: '违规信息类型分布',
        left: 'center',
        top: 10,
        textStyle: { fontSize: 16, fontWeight: 'bold' }
      },
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)'
      },
      legend: {
        orient: 'vertical',
        left: 'left',
        top: 60,
        data: blacklistData.value.reasons.map(item => item.name)
      },
      series: [
        {
          name: '违规类型',
          type: 'pie',
          radius: ['40%', '70%'],
          center: ['60%', '60%'],
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '18',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: blacklistData.value.reasons
        }
      ]
    }

    blacklistreasonsChart.setOption(option)
  } catch (error) {
    console.error('初始化违规信息图表失败:', error)
  }
}

// 初始化热点信息统计图表
const initHotspotChart = async () => {
  await getHotPost()
  const chartDom = document.getElementById('hotspotChart')
  if (!chartDom) {
    console.warn('hotspotChart DOM element not found')
    return
  }

  try {
    // 如果已存在实例，先销毁
    if (hotspotChart) {
      hotspotChart.dispose()
      hotspotChart = null
    }

    hotspotChart = echarts.init(chartDom)

    const option = {
      title: {
        text: '热点信息热度趋势',
        left: 'center',
        top: 10,
        textStyle: { fontSize: 16, fontWeight: 'bold' }
      },
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: hotspotData.value.series.legend,
        top: 40
      },
      grid: {
        top: 80,
        left: 60,
        right: 40,
        bottom: 40
      },
      xAxis: {
        type: 'category',
        data: hotspotData.value.series.xaxis
      },
      yAxis: {
        type: 'value'
      },
      series: hotspotData.value.series.series
      // [
      //   {
      //     name: '阅读量',
      //     type: 'line',
      //     data: hotspotData.trend.map(item => item.reads),
      //     itemStyle: { color: '#2ed573' }
      //   },
      //   {
      //     name: '评论量',
      //     type: 'line',
      //     data: hotspotData.trend.map(item => item.comments),
      //     itemStyle: { color: '#1e90ff' }
      //   },
      //   {
      //     name: '转发量',
      //     type: 'line',
      //     data: hotspotData.trend.map(item => item.shares),
      //     itemStyle: { color: '#ff6348' }
      //   }
      // ]
    }

    hotspotChart.setOption(option)
  } catch (error) {
    console.error('初始化热点信息图表失败:', error)
  }
}

// 初始化热门发帖人统计图表
const initPostersChart = async () => {
  await getHotPoster()
  const chartDom = document.getElementById('postersChart')
  if (!chartDom) {
    console.warn('postersChart DOM element not found')
    return
  }

  try {
    // 如果已存在实例，先销毁
    if (postersChart) {
      postersChart.dispose()
      postersChart = null
    }

    postersChart = echarts.init(chartDom)

    const option = {
      title: {
        text: '热门发帖人活跃度',
        left: 'center',
        top: 10,
        textStyle: { fontSize: 16, fontWeight: 'bold' }
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { type: 'shadow' }
      },
      legend: {
        data: ['发帖数', '获赞数', '评论数'],
        top: 40
      },
      grid: {
        top: 80,
        left: 60,
        right: 40,
        bottom: 40
      },
      xAxis: {
        type: 'category',
        data: postersData.value.users.map(user => user.name)
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          name: '发帖数',
          type: 'bar',
          data: postersData.value.users.map(user => user.posts),
          itemStyle: { color: '#ffa502' }
        },
        {
          name: '获赞数',
          type: 'bar',
          data: postersData.value.users.map(user => user.likes),
          itemStyle: { color: '#2ed573' }
        },
        {
          name: '评论数',
          type: 'bar',
          data: postersData.value.users.map(user => user.comments),
          itemStyle: { color: '#1e90ff' }
        }
      ]
    }

    postersChart.setOption(option)
  } catch (error) {
    console.error('初始化热门发帖人图表失败:', error)
  }
}

// 初始化热门主题版块统计图表
const initTopicsChart = async () => {
  await getHotTopic()
  const chartDom = document.getElementById('topicsChart')
  if (!chartDom) {
    console.warn('topicsChart DOM element not found')
    return
  }

  try {
    // 如果已存在实例，先销毁
    if (topicsChart) {
      topicsChart.dispose()
      topicsChart = null
    }

    topicsChart = echarts.init(chartDom)

    const option = {
      title: {
        text: '热门主题版块活跃度',
        left: 'center',
        top: 10,
        textStyle: { fontSize: 16, fontWeight: 'bold' }
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: { type: 'shadow' }
      },
      legend: {
        data: ['帖子数', '用户数', '活跃度'],
        top: 40
      },
      grid: {
        top: 80,
        left: 60,
        right: 60,
        bottom: 40
      },
      xAxis: {
        type: 'category',
        data: topicsData.value.topics.map(topic => topic.name)
      },
      yAxis: [
        {
          type: 'value',
          name: '数量',
          position: 'left'
        },
        {
          type: 'value',
          name: '活跃度',
          position: 'right',
          max: 100
        }
      ],
      series: [
        {
          name: '帖子数',
          type: 'bar',
          data: topicsData.value.topics.map(topic => topic.posts),
          itemStyle: { color: '#ffa502' }
        },
        {
          name: '用户数',
          type: 'bar',
          data: topicsData.value.topics.map(topic => topic.users),
          itemStyle: { color: '#2ed573' }
        },
        {
          name: '活跃度',
          type: 'line',
          yAxisIndex: 1,
          data: topicsData.value.topics.map(topic => topic.activity),
          itemStyle: { color: '#ff6348' }
        }
      ]
    }

    topicsChart.setOption(option)
  } catch (error) {
    console.error('初始化热门主题图表失败:', error)
  }
}

// 刷新统计数据
const refreshData = () => {
  loading.value = true
  // setTimeout(() => {
  // 模拟数据刷新
  // loading.value = false
  // 重新初始化图表
  initCharts()
  // }, 1000)
}

// 导出统计数据
const exportData = () => {
  loading.value = true
  // proxy.$http({
  //   url: proxy.$http.adornUrl("/admin/forum-statistics/export"),
  //   method: "get",
  //   params: proxy.$http.adornParams({
  //     startDate: setDate(),
  //     endDate: dayjs().format('YYYY-MM-DD')
  //   }),
  //   responseType: 'blob'
  // })
  //   .then(({ data }) => {
  //     // 创建一个Blob对象
  //     const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
  //     // 创建一个下载链接
  //     const url = window.URL.createObjectURL(blob)
  //     const link = document.createElement('a')
  //     link.href = url
  //     link.download = `论坛统计数据_${setDate()}-${dayjs().format('YYYY-MM-DD')}.xlsx`
  //     document.body.appendChild(link)
  //     link.click()
  //     // 清理
  //     document.body.removeChild(link)
  //     window.URL.revokeObjectURL(url)

  //     ElMessage.success('导出成功')
  //   }).catch(() => {
  //     ElMessage.error('导出失败')
  //   }).finally(() => {
  //     loading.value = false
  //   })






    
}

// 初始化所有图表
const initCharts = () => {
  setTimeout(() => {
    // 只初始化当前选中的tab对应的图表
    switch (selectedDimension.value) {
      case 'violation':
        initViolationChart()
        break
      case 'blacklist':
        initBlacklistChart()
        break
      case 'hotspot':
        initHotspotChart()
        break
      case 'posters':
        initPostersChart()
        break
      case 'topics':
        initTopicsChart()
        break
    }
  }, 200)
}

// 监听窗口大小变化
const handleResize = () => {
  const charts = [violationChart, blacklistChart, hotspotChart, postersChart, topicsChart]
  charts.forEach(chart => {
    if (chart && typeof chart.resize === 'function') {
      try {
        chart.resize()
      } catch (error) {
        console.warn('图表resize失败:', error)
      }
    }
  })
}

onMounted(() => {
  // getDataList()
  // 延迟初始化，确保DOM完全渲染
  setTimeout(() => {
    initCharts()
  }, 300)
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  const charts = [violationChart, blacklistChart, hotspotChart, postersChart, topicsChart]
  charts.forEach(chart => {
    if (chart && typeof chart.dispose === 'function') {
      try {
        chart.dispose()
      } catch (error) {
        console.warn('图表dispose失败:', error)
      }
    }
  })
})
</script>

<template>
  <div class="forum-statistics">
    <div class="statistics-header">
      <h1 class="page-title">论坛信息统计分析</h1>
      <div class="header-actions">
        <el-select v-model="timeRange" placeholder="选择时间范围" style="width: 150px; margin-right: 16px;"
          @change="refreshData">
          <el-option label="最近7天" value="week" />
          <el-option label="最近30天" value="month" />
          <el-option label="最近90天" value="quarter" />
        </el-select>
        <el-button type="primary" @click="refreshData"  :icon="Refresh">
          刷新数据
        </el-button>
        <!-- <el-button type="success" @click="exportData" :loading="loading"  :icon="Download">
          导出数据
        </el-button> -->
      </div>
    </div>

    <el-tabs v-model="selectedDimension" class="statistics-tabs">
      <!-- 违规信息统计 -->
      <el-tab-pane label="违规信息统计" name="violation">
        <div class="statistics-content">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-card class="chart-card">
                <div id="violationChart" style="height: 500px;"></div>
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="data-card">
                <template #header>
                  <div class="card-header">
                    <span>违规信息概览</span>
                  </div>
                </template>
                <div class="overview-stats">
                  <div class="stat-item">
                    <div class="stat-number">{{ violationData.total || '--' }}</div>
                    <div class="stat-label">总违规数</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-number">{{ violationData.pending || '--' }}</div>
                    <div class="stat-label">待处理</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-number">{{ violationData.processed || '--' }}</div>
                    <div class="stat-label">已处理</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-number">{{ violationData.rejected || '--' }}</div>
                    <div class="stat-label">已驳回</div>
                  </div>
                </div>
                <div class="custom-list horizontal-list">
                  <div class="list-header">
                    <span class="header-title">违规类型详情</span>
                  </div>
                  <div class="list-content">
                    <div v-for="(item, index) in violationData.categories" :key="item.name"
                      class="list-item horizontal-item" :class="{ 'list-item-highlight': index < 3 }">
                      <div class="item-main">
                        <div class="item-rank">{{ index + 1 }}</div>
                        <div class="item-info">
                          <div class="item-name">{{ item.name }}</div>
                          <div class="item-desc">违规类型</div>
                        </div>
                      </div>
                      <div class="item-stats">
                        <div class="stat-value">{{ item.value }}</div>
                        <div class="stat-percentage">{{ item.percentage }}%</div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>

      <!-- 黑名单人员统计 -->
      <el-tab-pane label="黑名单人员统计" name="blacklist">
        <div class="statistics-content">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-card class="chart-card">
                <div id="blacklistChart" style="height: 500px;"></div>
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="data-card">
                <template #header>
                  <div class="card-header">
                    <span>黑名单概览</span>
                  </div>
                </template>
                <div class="overview-stats">
                  <div class="stat-item">
                    <div class="stat-number">{{ blacklistData.total }}</div>
                    <div class="stat-label">总黑名单数</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-number">{{ blacklistData.mutedUsers }}</div>
                    <div class="stat-label">禁用</div>
                  </div>
                  <div class="stat-item">
                    <div class="stat-number">{{ blacklistData.disabledUsers }}</div>
                    <div class="stat-label">禁言</div>
                  </div>
                </div>
                <div class="custom-list horizontal-list">
                  <div class="list-header">
                    <span class="header-title">封禁原因分析</span>
                  </div>
                  <div class="">
                    <div id="blacklistreasonsChart" style="height: 400px;"></div>
                    <!-- <div 
                      v-for="(item, index) in blacklistData.reasons" 
                      :key="item.name"
                      class="list-item horizontal-item"
                      :class="{ 'list-item-highlight': index < 2 }"
                    >
                      <div class="item-main">
                        <div class="item-rank">{{ index + 1 }}</div>
                        <div class="item-info">
                          <div class="item-name">{{ item.name }}</div>
                          <div class="item-desc">封禁原因</div>
                        </div>
                      </div>
                      <div class="item-stats">
                        <div class="stat-value">{{ item.value }}</div>
                        <div class="stat-percentage">{{ item.percentage }}%</div>
                      </div> -->
                    <!-- </div> -->
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>

      <!-- 热点信息统计 -->
      <el-tab-pane label="热点信息统计" name="hotspot">
        <div class="statistics-content">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-card class="chart-card">
                <div id="hotspotChart" style="height: 500px;"></div>
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="data-card">
                <template #header>
                  <div class="card-header">
                    <span>热点帖子排行</span>
                  </div>
                </template>
                <div class="custom-list horizontal-list">
                  <!-- <div class="list-header">
                    <span class="header-title">热门帖子TOP5</span>
                  </div> -->
                  <div class="list-content">
                    <div v-for="(item, index) in hotspotData.posts" :key="item.title"
                      class="list-item horizontal-item post-item" :class="{ 'list-item-highlight': index < 3 }">
                      <div class="item-main">
                        <div class="item-rank" :class="`rank-${index + 1}`">{{ index + 1 }}</div>
                        <div class="item-info">
                          <div class="item-name post-title">{{ item.title }}</div>
                          <div class="item-desc">热门帖子</div>
                        </div>
                      </div>
                      <div class="item-stats post-stats">
                        <div class="stat-row">
                          <span class="stat-label-small">阅读</span>
                          <span class="stat-value-small">{{ item.reads >= 1000 ? ((item.reads / 1000).toFixed(1) + 'k')
                            :
                            item.reads }}</span>
                        </div>
                        <div class="stat-row">
                          <span class="stat-label-small">评论</span>
                          <span class="stat-value-small">{{ item.comments }}</span>
                        </div>
                        <div class="stat-row">
                          <span class="stat-label-small">转发</span>
                          <span class="stat-value-small">{{ item.likes >= 1000 ? ((item.likes / 1000).toFixed(1) + 'k')
                            :
                            item.likes }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>

      <!-- 热门发帖人统计 -->
      <el-tab-pane label="热门发帖人统计" name="posters">
        <div class="statistics-content">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-card class="chart-card">
                <div id="postersChart" style="height: 500px;"></div>
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="data-card">
                <template #header>
                  <div class="card-header">
                    <span>热门发帖人排行</span>
                  </div>
                </template>
                <div class="custom-list horizontal-list">
                  <!-- <div class="list-header">
                    <span class="header-title">活跃用户TOP5</span>
                  </div> -->
                  <div class="list-content">
                    <div v-for="(item, index) in postersData.users" :key="item.name"
                      class="list-item horizontal-item user-item" :class="{ 'list-item-highlight': index < 3 }">
                      <div class="item-main">
                        <div class="item-rank" :class="`rank-${index + 1}`">{{ index + 1 }}</div>
                        <!-- <div class="user-avatar">
                          <img :src="item.avatar" :alt="item.name" />
                        </div> -->
                        <div class="item-info">
                          <div class="item-name">{{ item.name }}</div>
                          <div class="item-desc">活跃用户</div>
                        </div>
                      </div>
                      <div class="item-stats user-stats">
                        <div class="stat-row">
                          <span class="stat-label-small">发帖</span>
                          <span class="stat-value-small">{{ item.posts }}</span>
                        </div>
                        <div class="stat-row">
                          <span class="stat-label-small">获赞</span>
                          <span class="stat-value-small">{{ item.likes >= 1000 ? ((item.likes / 1000).toFixed(1) + 'k')
                            :
                            item.likes }}</span>
                        </div>
                        <div class="stat-row">
                          <span class="stat-label-small">评论数</span>
                          <span class="stat-value-small">{{ item.comments >= 1000 ? ((item.comments / 1000).toFixed(1) +
                            'k')
                            : item.comments }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>

      <!-- 热门主题版块统计 -->
      <el-tab-pane label="热门主题版块统计" name="topics">
        <div class="statistics-content">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-card class="chart-card">
                <div id="topicsChart" style="height: 500px;"></div>
              </el-card>
            </el-col>
            <el-col :span="12">
              <el-card class="data-card">
                <template #header>
                  <div class="card-header">
                    <span>主题版块排行</span>
                  </div>
                </template>
                <div class="custom-list horizontal-list">
                  <!-- <div class="list-header">
                    <span class="header-title">热门主题TOP5</span>
                  </div> -->
                  <div class="list-content">
                    <div v-for="(item, index) in topicsData.topics" :key="item.name"
                      class="list-item horizontal-item topic-item" :class="{ 'list-item-highlight': index < 3 }">
                      <div class="item-main">
                        <div class="item-rank" :class="`rank-${index + 1}`">{{ index + 1 }}</div>
                        <div class="item-info">
                          <div class="item-name post-title">{{ item.name }}</div>
                          <div class="item-desc">主题版块</div>
                        </div>
                      </div>
                      <div class="item-stats topic-stats">
                        <div class="stat-row">
                          <span class="stat-label-small">帖子</span>
                          <span class="stat-value-small">{{ item.posts >= 1000 ? ((item.posts / 1000).toFixed(1) + 'k')
                            :
                            item.posts }}</span>
                        </div>
                        <div class="stat-row">
                          <span class="stat-label-small">用户</span>
                          <span class="stat-value-small">{{ item.users >= 1000 ? ((item.users / 1000).toFixed(1) + 'k')
                            :
                            item.users }}</span>
                        </div>
                        <div class="stat-row">
                          <span class="stat-label-small">活跃度</span>
                          <span class="stat-value-small activity">{{ item.activity }}%</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<style scoped lang="scss">
.forum-statistics {
  padding: 24px;
  background: #f5f7fa;
  min-height: 100vh;
}

.statistics-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.page-title {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin: 0;
}

.header-actions {
  display: flex;
  align-items: center;
}

.statistics-tabs {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.statistics-content {
  padding: 20px;
}

.chart-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.data-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  font-weight: bold;
  color: #303133;
}

.overview-stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 20px;
}

.stat-item {
  text-align: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  min-width: 100px;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 8px;
}

.stat-label {
  font-size: 14px;
  color: #606266;
}

// 自定义列表样式
.custom-list {
  // margin-top: 20px;
}

.list-header {
  padding: 12px 0;
  border-bottom: 1px solid #ebeef5;
  margin-bottom: 16px;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.list-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

// 横向布局样式
.horizontal-list {
  .list-content {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .horizontal-item {
    padding: 12px;
    min-height: 80px;

    .item-main {
      margin-bottom: 8px;
    }

    .item-stats {
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
    }
  }
}

.list-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  background: #fafbfc;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
  transition: all 0.3s ease;
  cursor: pointer;

  &:hover {
    background: #f0f9ff;
    border-color: #409eff;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.15);
  }

  &.list-item-highlight {
    background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
    border-color: #2196f3;
    box-shadow: 0 2px 8px rgba(33, 150, 243, 0.2);
  }
}

.item-main {
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
}

.item-rank {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #e4e7ed;
  color: #606266;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 14px;
  margin-right: 12px;
  flex-shrink: 0;

  &.rank-1 {
    background: linear-gradient(135deg, #ffd700 0%, #ffed4e 100%);
    color: #8b4513;
  }

  &.rank-2 {
    background: linear-gradient(135deg, #c0c0c0 0%, #e0e0e0 100%);
    color: #4a4a4a;
  }

  &.rank-3 {
    background: linear-gradient(135deg, #cd7f32 0%, #daa520 100%);
    color: #fff;
  }
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 12px;
  flex-shrink: 0;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-name {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  &.post-title {
    font-size: 13px;
    line-height: 1.4;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
}

.item-desc {
  font-size: 12px;
  color: #909399;
}

.item-stats {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}

.stat-value {
  font-size: 16px;
  font-weight: bold;
  color: #409eff;
}

.stat-percentage {
  font-size: 12px;
  color: #67c23a;
  font-weight: 500;
}

// 帖子统计样式
.post-stats {
  display: flex;
  gap: 12px;
}

// 用户统计样式
.user-stats {
  display: flex;
  gap: 12px;
}

// 主题统计样式
.topic-stats {
  display: flex;
  gap: 12px;
}

.stat-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-label-small {
  font-size: 10px;
  color: #909399;
}

.stat-value-small {
  font-size: 12px;
  font-weight: 600;
  color: #303133;

  &.activity {
    color: #67c23a;
  }
}

:deep(.el-tabs__header) {
  margin: 0;
  padding: 0 20px;
  background: #f8f9fa;
  border-radius: 8px 8px 0 0;
}

:deep(.el-tabs__content) {
  padding: 0;
}

:deep(.el-card__body) {
  padding: 20px;
}
</style>
